package io.nads.utils;

import io.nads.context.BaseContext;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class MessageIdWorker {
    private static final long START_TIMESTAMP = 1758985954L;
    public static final int COUNT_BITS = 32;

    /**
     * 生成全局唯一的ID，由时间戳+用户Id
     *
     * @return 全局唯一ID
     */
    public static String generateId() {
        // 生成时间戳
        LocalDateTime now = LocalDateTime.now();
//        long nowStamp = now.toEpochSecond(ZoneOffset.UTC);

        long nowStamp = System.currentTimeMillis(); // 毫秒级
        long timeStamp = nowStamp - START_TIMESTAMP;

        // 拼接成id
        return String.valueOf(timeStamp << COUNT_BITS | BaseContext.getCurrentId());
    }
}
